<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/8
 * Time: 13:46
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\GoodsModel;
use think\Db;

class GoodsController extends AdminBaseController
{
    //商品列表页面
    public function index()
    {      
        /**搜索条件**/
        $goodsname = $this->request->param('g_goodsname');
        $code = trim($this->request->param('g_code'));
        $where=array();
        if ($goodsname) {
            $where['g_goodsname'] = ['like', "%$goodsname%"];
        }
        if ($code) {
            $where['g_code'] = ['like', "%$code%"];;
        }
        $goods = Db::name('Goods')
        ->where($where)
        ->order("g_id DESC")
        ->paginate(10);        
        $goods->appends(['g_goodsname' => $goodsname,'g_code' => $code]);
        // 获取分页显示
        $page = $goods->render(); 
        $this->assign("page", $page);
        $this->assign("data", $goods);
        return $this->fetch();
    }
    //新增
    public function add()
    {
        //获取单位
        $unit = Db::name('Unit')->where(array('status'=>1))->select();
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();
        $this->assign("unit", $unit);
        $this->assign("classify", $classify);
        return $this->fetch();
    }
    public function addPost(){   
        if ($this->request->isPost()) {                       
            $res  = $this->request->param();             
            if (!empty($res['photo_names']) && !empty($res['photo_urls'])) {
                $res['post']['more']['photos'] = [];
                foreach ($res['photo_urls'] as $key => $url) {
                    $photoUrl = cmf_asset_relative_url($url);
                    array_push($res['post']['more']['photos'], ["url" => $photoUrl, "name" => $res['photo_names'][$key]]);
                }
            }
            if(empty($res['options']['g_goodsname'])){
                $this->error("商品名称不能为空！");
                exit;
            }
            if(empty($res['options']['g_code'])){
                $this->error("拼音简码不能为空！");
                exit;
            }
           $data['g_goodsname']=$res['options']['g_goodsname'];
           $data['g_pic']=$res['photo_urls'][0];
           $data['g_unit']=$res['g_unit'];
           $data['g_code']=$res['options']['g_code'];
           $data['g_category']=$res['g_category'];
           $data['g_nowprice']=isset($res['g_nowprice'])?$res['g_nowprice']:0;
           $data['g_meal']=isset($res['g_meal'])?$res['g_meal']:0;
           $data['g_agio']=isset($res['g_agio'])?$res['g_agio']:0;
           $data['buy_get']=isset($res['buy_get'])?$res['buy_get']:0;
           $data['even']=isset($res['even'])?$res['even']:0;
           $data['special_offer']=isset($res['special_offer'])?$res['special_offer']:0;
           $data['g_ctime']=time();          
           $re=Db::name('Goods')->insert($data);
           if($re){
               $this->success("添加成功！", url("Goods/index"));
           }else{
               $this->error("添加失败，请重新添加！", url("Goods/add"));
           }
        }         
    } 
    //编辑
    public function edit()
    {
        $id = $this->request->param("g_id");
        $data=Db::name('Goods')->where(array('g_id'=>$id))->find();
        //获取单位
        $unit = Db::name('Unit')->where(array('status'=>1))->select();
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();
        //$GoodsModel = new GoodsModel();
        //$data['unit1']=$GoodsModel->getUname($data['g_unit']);
       //dump($data);
        $this->assign("unit", $unit);
        $this->assign("classify", $classify);        
        $this->assign("data", $data);
        return $this->fetch();
    }
    
    public function editPost(){
        if ($this->request->isPost()) {
            $res  = $this->request->param();
            if (!empty($res['photo_names']) && !empty($res['photo_urls'])) {
                $res['post']['more']['photos'] = [];
                foreach ($res['photo_urls'] as $key => $url) {
                    $photoUrl = cmf_asset_relative_url($url);
                    array_push($res['post']['more']['photos'], ["url" => $photoUrl, "name" => $res['photo_names'][$key]]);
                }
            }
            $data['g_goodsname']=$res['options']['g_goodsname'];
            if(isset($res['photo_urls'][0])){
                $data['g_pic']=$res['photo_urls'][0];
            }
            $data['g_unit']=$res['g_unit'];
            $data['g_code']=$res['options']['g_code'];
            $data['g_category']=$res['g_category'];
            $data['g_nowprice']=isset($res['g_nowprice'])?$res['g_nowprice']:0;
            $data['g_meal']=isset($res['g_meal'])?$res['g_meal']:0;
            $data['g_agio']=isset($res['g_agio'])?$res['g_agio']:0;
            $data['buy_get']=isset($res['buy_get'])?$res['buy_get']:0;
            $data['even']=isset($res['even'])?$res['even']:0;
            $data['special_offer']=isset($res['special_offer'])?$res['special_offer']:0;
            $data['g_utime']=time();
            $re=Db::name('Goods')->where(array('g_id'=>$res['g_id']))->update($data);
            if($re){
                $this->success("修改成功！", url("Goods/index"));
            }else{
                $this->error("修改失败！", url("Goods/edit"));
            }
        }
    }
    
    
    
    //删除商品
    public function delete()
    {
        $id = $this->request->param("g_id");
        $info = Db::name('Goods')->where(['g_id' => $id])->find();
        if ($info){
            $status = Db::name('Goods')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", url('Goods/index'));
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("该品项不存在！");
        }
    }
    public function upStatus()
    {
        $intId     = $this->request->param("g_id");
        $intStatus = $this->request->param("g_status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("请选择要修改数据"));
        }
        $GoodsModel = new GoodsModel();
        $GoodsModel->isUpdate(true)->save(["g_status" => $intStatus], ["g_id" => $intId]);
        $this->success(lang("修改成功！"));
    }   
}